package com.sirdc.modules.sys.web.threeSevenAndInterView;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.interview.entity.InterviewRegister;
import net.huashitong.interview.filter.InterviewRegisterFilter;
import net.huashitong.interview.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("web/interview")
public class WebInterViewController extends JsonBaseController {

    @Autowired
    private RegisterService service;
    @Override
    protected String getView(String s) {
        return null;
    }

    /**
     * 登记 领导页
     * @param filter (2:永泰县委,3:永泰县人大,4:永泰县政府,5:永泰县政协)
     *               fillUnitId、fillDate
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public Message getRegister(@RequestBody InterviewRegisterFilter filter){
        if(ObjectUtils.isNotBlank(filter.getSearchWord())){
            filter.setFillUnitId(null);
        }
        if(ObjectUtils.isBlank(filter.getFillUnitId())){
            filter.setFillUnitId("2");
        }
        String fillDate = "";
        if(ObjectUtils.isBlank(filter.getFillDate())){
            //获取数据库中最新的时间
            fillDate = service.getNewFillDate();
            filter.setFillDate(fillDate.substring(0,6));
        }else{
            fillDate = filter.getFillDate();
        }
        List<InterviewRegister> list = service.queryRegister(filter);
        if(list.size()==0){
            return coverMessage("200", "无数据");
        }
        List<Map<String, Object>> resultList = service.baseResult(list);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("fillDate",fillDate);
        map2.put("year",fillDate.substring(0,4));
        map2.put("month",fillDate.substring(4,6));
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("list",resultList);
        resultMap.put("time",map2);
        return coverMessage("200", "获取成功", resultMap);
    }

    /**
     * 登记 领导页添加
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addregister",method = RequestMethod.POST)
    public Message addRegister(@RequestBody InterviewRegister filter){
        service.save(filter);
        return coverMessage("200","添加成功");
    }

    /**
     * 登记 领导页搜索
     * @param filter (searchWord)
     * @return
     */
//    @ResponseBody
//    @RequestMapping(value = "/searchregister",method = RequestMethod.POST)
//    public Message searchRegister(@RequestBody InterviewRegisterFilter filter){
//
//        List<InterviewRegister> list = service.searchRegister(filter);
//        if(list.size()==0){
//            return coverMessage("201", "无数据");
//        }
//        List<Map<String, Object>> result = service.baseResult(list);
//        return coverMessage("200", "搜索成功", result);
//    }

    /**
     * 登记 详情页
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getregisterbyid",method = RequestMethod.POST)
    public Message getRegisterById(@RequestBody InterviewRegisterFilter filter){
        InterviewRegister register = service.getById(filter.getSysId());
        Map<String,String> map = new HashMap<>();

        map.put("sysId",filter.getSysId());
        map.put("fillUnitId",ObjectUtils.isNotBlank(register.getFillUnitId())?register.getFillUnitId():"-");
        map.put("userName",ObjectUtils.isNotBlank(register.getUserName())?register.getUserName():"-");
        map.put("contactBusiness",ObjectUtils.isNotBlank(register.getContactBusiness())?register.getContactBusiness():"-");
        map.put("interviewCountThisMonth",ObjectUtils.isNotBlank(register.getInterviewCountThisMonth())?register.getInterviewCountThisMonth():"-");
        map.put("businessProblem",ObjectUtils.isNotBlank(register.getBusinessProblem())?register.getBusinessProblem():"-");
        map.put("solveState",ObjectUtils.isNotBlank(register.getSolveState())?register.getSolveState():"-");
        map.put("fillDate",ObjectUtils.isNotBlank(register.getFillDate())?register.getFillDate():"-");
        map.put("year",ObjectUtils.isNotBlank(register.getFillDate())?register.getFillDate().substring(0,4):"-");
        map.put("month",ObjectUtils.isNotBlank(register.getFillDate())?register.getFillDate().substring(4,6):"-");
        map.put("helpExperience",ObjectUtils.isNotBlank(register.getHelpExperience())?register.getHelpExperience():"-");

        return coverMessage("200","获取成功",map);
    }

    /**
     * 登记 修改页
     * @param interviewRegister
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/editregister",method = RequestMethod.POST)
    public Message editRegister(@RequestBody InterviewRegister interviewRegister){
        InterviewRegister newBean = service.getById(interviewRegister.getSysId());
        if(ObjectUtils.isNotBlank(interviewRegister.getInterviewCountThisMonth())){
            newBean.setInterviewCountThisMonth(interviewRegister.getInterviewCountThisMonth());
        }
        if(ObjectUtils.isNotBlank(interviewRegister.getUserName())){
            newBean.setUserName(interviewRegister.getUserName());
        }
        if(ObjectUtils.isNotBlank(interviewRegister.getFillUnitId())){
            newBean.setFillUnitId(interviewRegister.getFillUnitId());
        }
        if(ObjectUtils.isNotBlank(interviewRegister.getFillDate())){
            newBean.setFillDate(interviewRegister.getFillDate());
        }
        if(ObjectUtils.isNotBlank(interviewRegister.getContactBusiness())){
            newBean.setContactBusiness(interviewRegister.getContactBusiness());
        }
        if(ObjectUtils.isNotBlank(interviewRegister.getBusinessProblem())){
            newBean.setBusinessProblem(interviewRegister.getBusinessProblem());
        }
        if(ObjectUtils.isNotBlank(interviewRegister.getHelpExperience())){
            newBean.setHelpExperience(interviewRegister.getHelpExperience());
        }
        if(ObjectUtils.isNotBlank(interviewRegister.getSolveState())){
            newBean.setSolveState(interviewRegister.getSolveState());
        }
        service.update(newBean);
        return coverMessage("200", "修改成功");
    }


    /**
     * 登记 删除功能
     * @param interviewRegister
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteregister",method = RequestMethod.POST)
    public Message deleteRegister(@RequestBody InterviewRegister interviewRegister){
        service.deleteById(interviewRegister.getSysId());
        return coverMessage("200", "删除成功");
    }

}
